package org.elfen.redis;

import java.io.Serializable;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import org.elfen.common.util.SpringContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;


@Component
public class RedisUtil {
    @Autowired
    private RedisTemplate redisTemplate;
    
    public RedisTemplate getRedis(){
    	if(this.redisTemplate==null){
    		System.err.println("--redisTemplate 为空！---");
    		this.redisTemplate = SpringContextUtil.getBean("redisTemplate");
    		System.out.println(redisTemplate);
    	}
    	return this.redisTemplate;
    }
    
    /**
     * 推入消息到redis的指定的消息队列中【左进】
     * @param queueName
     * @param message
     */
    public void publish(String queueName,String message){
    	try {
			ListOperations<Serializable, String> opsList = this.getRedis().opsForList();
			opsList.leftPush(queueName, message);
		} catch (Exception e) {
			e.printStackTrace();
		}
    }
    
    /**
     * 消费指定的消息队列的消息【右出】
     * @param queueName
     * @return message 
     */
    public String subscribe(String queueName){
    	ListOperations<Serializable, String> opsList = this.getRedis().opsForList();
    	String message = opsList.rightPop(queueName);
    	return message;
    }
    
    /**
     * 批量删除对应的value
     *
     * @param keys
     */
    public void remove(final String... keys) {
        for (String key : keys) {
            remove(key);
        }
    }

    /**
     * 批量删除key
     *
     * @param pattern
     */

    public void removePattern(final String pattern) {
        Set<Serializable> keys = getRedis().keys(pattern);
        if (keys.size() > 0)
            getRedis().delete(keys);
    }

    /**
     * 删除对应的value
     *
     * @param key
     */

    public void remove(final String key) {
        if (exists(key)) {
            getRedis().delete(key);
        }
    }

    /**
     * 判断缓存中是否有对应的value
     *
     * @param key
     * @return
     */

    public boolean exists(final String key) {
        return getRedis().hasKey(key);
    }

    /**
     * 读取缓存
     *
     * @param key
     * @return
     */

    public Object get(final String key) {
        Object result = null;
        ValueOperations<Serializable, Object> operations = getRedis().opsForValue();
        result = operations.get(key);
        return result;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @return
     */

    public boolean set(final String key, Object value) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = getRedis().opsForValue();
            operations.set(key, value);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 写入缓存
     *
     * @param key
     * @param value
     * @param expireTime seconds
     * @return
     */

    public boolean set(final String key, Object value, Long expireTime) {
        boolean result = false;
        try {
            ValueOperations<Serializable, Object> operations = getRedis().opsForValue();
            operations.set(key, value);
            getRedis().expire(key, expireTime, TimeUnit.SECONDS);
            result = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }
}